#!/bin/bash
# 手动初始化数据库表结构
# 用途：在不启动 Event Processor 的情况下初始化数据库

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
SCHEMA_FILE="$PROJECT_ROOT/event-processor/schema.sql"

echo "🗄️  初始化数据库表结构..."
echo "=================================================="

# 检查 schema.sql 是否存在
if [ ! -f "$SCHEMA_FILE" ]; then
    echo "❌ schema.sql 文件不存在: $SCHEMA_FILE"
    exit 1
fi

# 检查 PostgreSQL 容器是否运行
if ! docker ps --filter "name=browse-etl-postgres" --format "{{.Names}}" | grep -q "browse-etl-postgres"; then
    echo "❌ PostgreSQL 容器未运行，请先启动: make start"
    exit 1
fi

# 执行 SQL 文件
echo "📝 执行 schema.sql..."
docker exec -i browse-etl-postgres psql -U postgres -d behavior_analysis < "$SCHEMA_FILE"

echo ""
echo "=================================================="
echo "✅ 数据库表初始化完成！"
echo ""
echo "📊 查看表结构:"
echo "   docker exec browse-etl-postgres psql -U postgres -d behavior_analysis -c '\d user_behaviors'"
echo ""
echo "📝 查看数据:"
echo "   docker exec browse-etl-postgres psql -U postgres -d behavior_analysis -c 'SELECT * FROM user_behaviors LIMIT 10;'"
echo "=================================================="

